package com.pay.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 *
 * 异步回调
 * Created by chenweiyi on 2018/2/8.
 */
public class PayTestServlet extends HttpServlet{

    private PrintWriter writer;

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doPost(req, resp);
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String uri = req.getRequestURI();
        String contextPath = req.getContextPath();
        if(uri.startsWith(contextPath)){
            uri = uri.substring(contextPath.length());
        }
        if(uri.equals("/notify.do")){
            String code = req.getParameter("code");
            String result_code = req.getParameter("result_code");
            writer = resp.getWriter();
            if("SUCCESS".equals(code)){
                /**
                 * 省略验证签名
                 */
                if("SUCCESS".equals(result_code)){

                    writer.write("success");
                }

            }else{
                writer.write("failure");
            }

        }

    }
}
